var util = require("../../../utils/util.js"),
    {
        api
    } = require("../../../utils/api.js"),
    app = getApp();
Page({

    /**
     * 页面的初始数据
     */
    data: {
        datetimeShow: false,
        calendarShow: false,
        conformShow: false,
        currentDate: '',
        currentTime: '',
        name: '',
        mobile: '',
        problem: '',
        store_id: '',
        agencyShow: false,
        agencyColumns: [],
        institution_name: '',
        multiArray: [],
        cityIndex: [0, 0],
        cityList: [],
        minHour: 0,
        minMinute: 0,
        orignMinMinute: 0,
        maxMinute: 60,
        dateMultiArray: [],
        dateMityIndex: [0, 0],
        dateMultiArray1:['上午','下午']
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.setData({
            store_id: options.store_id || ''
        })
        wx.hideShareMenu();
        this.patientStortList();
    },

    showDate() {
        this.setData({
            calendarShow: true
        })
    },
    showTime() {
        this.setData({
            datetimeShow: true
        })
    },
    onClose() {
        this.setData({
            datetimeShow: false,
            agencyShow: false,
            conformShow: false,
            calendarShow: false
        })
    },
    onClose1() {
        this.setData({
            conformShow: false
        })
        clearTimeout(this.timer)
        wx.navigateBack()
    },
    onConfirm(e) {
        this.setData({
            calendarShow: false,
            currentDate: util.formatTime2(e.detail)
        })
        this.setDateMin()
    },
    setDateMin() {
        let currentDate = this.data.currentDate;
        let nowDate = util.formatTime2(new Date());
        let lineTime = [0, 15, 30, 45];
        let hour = [];
        if (currentDate == nowDate) {
            let nowHour = new Date().getTime() + 3600 * 1000;
            nowHour = new Date(nowHour).getHours()
            let minu = new Date().getMinutes();
            if(minu>45){
                nowHour=nowHour+1;
                minu=0;
            }
            if(nowHour>20){
                return wx.showModal({
                  title: '温馨提示',
                  content: '今日已无可预约时间，请选择其它日期',
                  showCancel:false,
                  complete: (res) => {
                    if (res.confirm) {
                      wx.navigateBack();
                    }
                  }
                })
            }
            for (let i = nowHour; i <= 20; i++) {
                let minute = [];
                for (let j = 0; j < lineTime.length; j++) {
                    if (i == nowHour) {
                        if (lineTime[j] > minu) {
                            minute.push(lineTime[j])
                        }
                    } else if (i == 20) {
                        minute = [0, 15]
                    } else {
                        minute = lineTime;
                    }
                }
                hour.push({
                    val: i,
                    minute
                })
            }
        } else {
            for (let i = 8; i <= 20; i++) {
                let minute = [];
                if (i == 20) {
                    minute = [0, 15]
                } else {
                    minute = lineTime;
                }
                hour.push({
                    val: i,
                    minute
                })
            }
        }
        this.setData({
            dateMultiArray: [hour.map(o => o.val), hour.length>0?hour[0].minute:[]],
            allHour: hour
        })
    },
    datetimeConfirm(e) {
        this.setData({
            currentTime: e.detail,
            datetimeShow: false
        })
    },
    datetimeCancel() {
        this.setData({
            currentTime: '',
            datetimeShow: false
        })
    },
    getName(e) {
        this.setData({
            name: e.detail.value
        })
    },
    getMobile(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    getProblem(e) {
        this.setData({
            problem: e.detail.value
        })
    },
    onAgencyChange(e) {
        this.setData({
            store_id: this.data.agencyList[e.detail.index].id,
            institution_name: e.detail.value,
            agencyShow: false
        })
    },
    showAgency() {
        this.setData({
            agencyShow: true
        })
    },
    async getAreaList() {
        let {
            data
        } = await api.getAreaList();
        let areaList = Array.isArray(data.area_list) ? data.area_list : [],
            cityList = [],
            storeAllList = [];
        areaList.forEach(item => {
            let child = item.child && Array.isArray(item.child) ? item.child : [];
            child.forEach(val => {
                if (util.regularConfig.isString(val)) {
                    cityList.push(val)
                    storeAllList.push({
                        area: val,
                        storeList: []
                    })
                }
            })
        })
        let institution_name = '',
            cityIndex = [0, 0];
        storeAllList.forEach(item => {
            this.data.agencyList.forEach(val => {
                if (item.area === val.area) {
                    item.storeList.push({
                        name: val.name,
                        storeId: val.id
                    })
                }
            })
        })

        if (this.data.store_id) {
            storeAllList.forEach((item, index) => {
                item.storeList.forEach((val, key) => {
                    if (val.storeId == this.data.store_id) {
                        institution_name = val.name;
                        cityIndex = [index, key]
                    }
                })
            })
        }


        this.setData({
            cityList,
            storeAllList,
            institution_name,
            cityIndex,
            multiArray: [cityList, (storeAllList.length > cityIndex[0] ? storeAllList[cityIndex[0]].storeList.map(o => o.name) : [])]
        })
    },
    bindMultiPickerColumnChange1(e) {
        let sIndex = e.detail.value,
            column = e.detail.column,
            allHour = this.data.allHour;
        let hours=allHour.map(o=>o.val);
        let minutes=allHour.length>sIndex?allHour[sIndex].minute:[];
        if (column == 0) {
            this.setData({
                dateMultiArray: [hours, minutes],
                dateMityIndex: [sIndex, 0]
            })
        } else if (column == 1) {
            this.data.dateMityIndex[1] = sIndex;
            this.setData({
                dateMityIndex: this.data.dateMityIndex
            })
        }
    },
    cityChange1() {
        let dateMityIndex= this.data.dateMityIndex;
        let allHour = this.data.allHour;
        let frist=dateMityIndex[0];
        let second=dateMityIndex[1];
        let hours=allHour.length>frist?allHour[frist].val:'';
        let minute=allHour.length>frist && allHour[frist].minute.length>second?allHour[frist].minute[second]:'';
        if (hours && minute) {
            this.setData({
                currentTime: hours+':'+minute
            })
        }
    },
    cityChange2(e) {
        this.setData({
            currentTime: this.data.dateMultiArray1[e.detail.value]
        })
    },
    bindMultiPickerColumnChange(e) {
        let sIndex = e.detail.value,
            column = e.detail.column,
            storeAllList = this.data.storeAllList;
        if (column == 0) {
            let areaList = []
            if (storeAllList[sIndex].storeList && Array.isArray(storeAllList[sIndex].storeList) && storeAllList[sIndex].storeList.length > 0) {
                areaList = storeAllList[sIndex].storeList.map(o => o.name);
            }
            this.setData({
                multiArray: [this.data.cityList, areaList],
                cityIndex: [sIndex, 0]
            })
        } else if (column == 1) {
            this.data.cityIndex[1] = sIndex;
            this.setData({
                cityIndex: this.data.cityIndex
            })
        }
    },
    cityChange() {
        const {
            storeAllList,
            cityIndex
        } = this.data;
        if (storeAllList.length > cityIndex[0] && storeAllList[cityIndex[0]].storeList.length > cityIndex[1]) {
            let item = storeAllList[cityIndex[0]].storeList[cityIndex[1]];
            this.setData({
                institution_name: item ? item.name : '',
                store_id: item ? item.storeId : '',
            })
        }
    },
    async patientStortList() {
        let {
            data
        } = await api.patientStortList();
        let list = data && Array.isArray(data.list) ? data.list : [];
        this.setData({
            agencyList: list
        })
        this.getAreaList();
    },
    async submit() {
        if (!util.regularConfig.mobile.test(this.data.mobile)) return wx.showToast({
            title: '电话格式错误',
            icon: 'none'
        })
        let {
            code
        } = await api.appointmentSubmit({
            name: this.data.name,
            mobile: this.data.mobile,
            appointment_time: this.data.currentDate + ' ' + this.data.currentTime + ':00',
            problem: this.data.problem,
            store_id: this.data.store_id
        });
        if (code == 1) {
            this.setData({
                conformShow: true
            })
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage(e) {
        return util.shareEvent(e)
      }
})